
package com.nuvo.beans;

import com.nuvo.entities.Proveedor;
import com.nuvo.services.ProveedorService;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;


@ManagedBean
@SessionScoped
public class ProveedorController implements Serializable{
    @EJB
    private ProveedorService proveedorService;
    
    private Proveedor selected;
    

    
    public ProveedorController() {
    }

    public Proveedor getSelected() {
        if(selected == null){
            selected = new Proveedor();
        
        }
        return selected;
    }
    
    public List<Proveedor> listado(){
        return proveedorService.findAll();
    }
    
     public String index() {
        return "/proveedor/index";
    }

    public String create() {
        selected = new Proveedor();
        return "/proveedor/new";
    } 

    public String agregar() {
        
        proveedorService.create(selected);
        return "/proveedor/index";
    } 
    
   

    public String edit(int codigo) {
        selected = proveedorService.find(codigo);
        return "/proveedor/edit";
    } 

    public String guardar() {
        
        proveedorService.edit(selected);
        return "/proveedor/index";
    } 

    public String eliminar(int codigo) {
        selected = proveedorService.find(codigo);
        proveedorService.remove(selected);
        return "/proveedor/index";
    }
    
    
    // --------------------- Métodos de Ayuda para acceder al Bean por otras Clases ---------------------

    public SelectItem[] getItemsAvailableSelectOne() {
        return getSelectItems(proveedorService.findAll(), true);
    }

    // Genera una lista con los items seleccionados (uno o muchos según selectOne). Para tablas relacionadas.
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {

        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "Seleccione uno");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;

    }

    // ------------------ Clase para conversiones, se deben implementar todos los métodos ------------------
    @FacesConverter(forClass = Proveedor.class)
    public static class ProveedorControllerConverter implements Converter {

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        } 

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        } 

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) { return null; }
            ProveedorController controller = (ProveedorController ) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "proveedorController ");
            return controller.proveedorService.find(getKey(value));
        } 

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) { return null; }
            if (object instanceof Proveedor) {
                Proveedor o = (Proveedor) object;
                return getStringKey(o.getId());
            }
            else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ProveedorController.class.getName()); }
        } 

    } 
    
    

}

    


